﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Drawing;

namespace SMA
{
    public class TransparentTextBox : System.Windows.Forms.TextBox
    {
        public TransparentTextBox()
        {
            this.SetStyle(ControlStyles.Opaque, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x20;
                return cp;
            }
        }
    }


    /// <summary>
    /// Migrated to C# from VB.NET (http://www.xtremevbtalk.com/showthread.php?t=231509)
    /// </summary>
    public class SimpleProgressBar : System.Windows.Forms.UserControl
    {
        public enum ProgressInfoEnum
        {
            No_Percentage_No_Text = 0,
            Percentage_Only,
            Text_Only,
            Percentage_And_Text
        };

        int M_Value = 0,
        M_Maximum = 100;
        Byte M_TransparentLevel = 255;
        Color M_BarColor = Color.Violet;
        Color M_ProgressBackColor = Color.AntiqueWhite;
        Boolean M_Border = false;
        ProgressInfoEnum M_ProgressInfo = ProgressInfoEnum.Percentage_Only;
        String M_DisplayText = "";
        
        public int thisWidth { get; set; }

        public SimpleProgressBar()
        {
            //MyBase.New()

            //This call is required by the Windows Form Designer.
            InitializeComponent();

            //Add any initialization after the InitializeComponent() call
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.Opaque, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.ContainerControl, true);

            base.BackColor = Color.Transparent;

        }

        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams cp = base.CreateParams;
        //        cp.ExStyle |= 0x20;
        //        return cp;
        //    }
        //}

        //UserControl overrides dispose to clean up the component list.
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            this.Width = thisWidth;
            base.Dispose(disposing);
        }

        //Required by the Windows Form Designer
        private System.ComponentModel.IContainer components;

        //NOTE: The following procedure is required by the Windows Form Designer
        //It can be modified using the Windows Form Designer.  
        //Do not modify it using the code editor.
        [System.Diagnostics.DebuggerStepThrough()]
        private void InitializeComponent()
        {
            //
            //SimpleProgressBar
            //
            this.Name = "SimpleProgressBar";
            this.Size = new System.Drawing.Size(250, 20);
        }

        [Browsable(true)]
        public String DisplayText
        {
            get { return M_DisplayText; }
            set
            {
                M_DisplayText = value;
                this.Refresh();
            }
        }

        [Browsable(true)]
        public ProgressInfoEnum ProgressInfo
        {
            get { return M_ProgressInfo; }
            set
            {
                M_ProgressInfo = value;
                this.Refresh();
            }
        }

        [Browsable(true)]
        public int Value
        {
            get { return M_Value; }
            set
            {
                if (value > M_Maximum) value = M_Maximum;
                M_Value = value;
                //RectangleF rect = CalculateBarProgressBarWidth();
                this.Width = (int)(thisWidth * M_Value / Maximum);
                this.Refresh();
            }
        }

        [Browsable(true)]
        public int Maximum
        {
            get { return M_Maximum; }
            set
            {
                if (value < 1) value = 1;
                M_Maximum = value;
                this.Refresh();
            }
        }

        [Browsable(true)]
        public Byte TransparentLevel
        {
            get { return M_TransparentLevel; }
            set
            {
                M_TransparentLevel = value;
                this.Refresh();
            }
        }

        [Browsable(true)]
        public Boolean Border
        {
            get { return M_Border; }
            set
            {
                M_Border = value;
                this.Refresh();
            }
        }

        [Browsable(true)]
        public Color BarColor
        {
            get { return M_BarColor; }
            set
            {
                M_BarColor = value;
                this.Refresh();
            }
        }

        [Browsable(true)]
        public Color ProgressBackColor
        {
            get { return M_ProgressBackColor; }
            set
            {
                M_ProgressBackColor = value;
                this.Refresh();
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            RectangleF rect;
            if (M_Value > 0)
            {

                rect = CalculateBarProgressBarWidth();

                SolidBrush TBrush = new SolidBrush(Color.FromArgb(M_TransparentLevel, M_ProgressBackColor));
                e.Graphics.FillRectangle(TBrush, 0, 0, this.Width, this.Height);
                TBrush.Dispose();

                if (M_Value == 1)
                {
                    rect.Width = 1;
                }

                SolidBrush SBrush = new SolidBrush(Color.FromArgb(M_TransparentLevel, M_BarColor));
                e.Graphics.FillRectangle(SBrush, rect);
                SBrush.Dispose();
            }

            if (M_Border)
            {
                Pen NPen = new Pen(Color.FromArgb(M_TransparentLevel, Color.Black));
                e.Graphics.DrawLine(NPen, 0, 0, this.Width - 1, 0);
                e.Graphics.DrawLine(NPen, this.Width - 1, 0, this.Width - 1, this.Height - 1);
                e.Graphics.DrawLine(NPen, 0, this.Height - 1, this.Width - 1, this.Height - 1);
                e.Graphics.DrawLine(NPen, 0, 0, 0, this.Height - 1);
                NPen.Dispose();
            }

            DrawText(e.Graphics);
        }

        private void DrawText(Graphics e)
        {
            int TextPercent = (int)(((double)M_Value / (double)M_Maximum) * 100f);
             SizeF strsize ;
             int OffsetY=0 ;
             SolidBrush blbla  = new SolidBrush(Color.Black);
             String TempString="" ;
            switch (M_ProgressInfo)
	            {
            case ProgressInfoEnum.Percentage_Only:
                    TempString = TextPercent + "%";
                break;
            case ProgressInfoEnum.Text_Only:
                    if( M_DisplayText != ""){
                   TempString = M_DisplayText;
                    }
                break;
            case ProgressInfoEnum.Percentage_And_Text:
                 if( M_DisplayText == ""){
                   TempString = TextPercent + "%";
                 } else {
                     TempString = M_DisplayText + " " + TextPercent + "%";
                 }
                break;
            default:
                    TempString = M_DisplayText + " " + TextPercent + "%";
                break;
	            }

              strsize = e.MeasureString(TempString, Font);

              e.DrawString(TempString, this.Font, blbla, (this.Width - strsize.Width) / 2, (this.Height - strsize.Height) / 2 - OffsetY);

              blbla.Dispose();
        }

        private RectangleF CalculateBarProgressBarWidth()
        {
            return new RectangleF(0, 0, (int)(thisWidth * M_Value / Maximum), this.Height);
        }


        public void Increment()
        {
            if (M_Value < M_Maximum) { Value += 1; };
            this.Refresh();
        }

        //protected override void OnLoad(EventArgs e)
        //{
        //    base.OnLoad(e);
        //    this.Load += new EventHandler(SimpleProgressBar_Load);
        //}
        //public void SimpleProgressBar_Load(System.Object sender, System.EventArgs e) // Handles MyBase.Load
        //{
        //    thisWidth = this.Width;
        //}
    }

}
